package com.wyl.mybatis.demo;

import com.wyl.mybatis.config.SqlSessionFactoryConfig;
import com.wyl.mybatis.entity.FullCity;
import com.wyl.mybatis.entity.User;
import com.wyl.mybatis.mapper.FullCityMapper;
import com.wyl.mybatis.mapper.UserMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.List;

/**
 * @Description
 * @Author WuYiLong
 * @Date 2024/3/22 21:16
 */
public class MoreDataSource {

    private static final Logger log = LoggerFactory.getLogger(MoreDataSource.class);

    public static void main(String[] args) throws IOException {
        // mysql
        SqlSessionFactory sqlSessionFactory = SqlSessionFactoryConfig.buildSqlSessionFactory();
        try (SqlSession sqlSession = sqlSessionFactory.openSession()) {

            FullCityMapper mapper = sqlSession.getMapper(FullCityMapper.class);
            FullCity fullCity = mapper.selectByName("广东省");
            System.out.println("区域名称：" + fullCity.getFullName());
        }

        // postgresql
        SqlSessionFactory pgsql = SqlSessionFactoryConfig.buildSqlSessionFactoryEnv("pgsql");
        try (SqlSession sqlSession = pgsql.openSession()) {
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            List<User> users = mapper.selectList();
            for (User user : users) {
                log.info("用户名称：{},密码：{}",user.getUsername(),user.getPassword());
            }
        }

    }
}